package cn.huapu.power.energy.service.impl;

import cn.huapu.power.energy.bean.PeaFrequency;
import cn.huapu.power.energy.bean.SysClientEng;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.mapper.PeaFrequencyMapper;
import cn.huapu.power.energy.mapper.SysClientEngMapper;
import cn.huapu.power.energy.service.PeaFrequencyService;
import cn.huapu.power.energy.util.ObjectHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 测试用户权限服务实现类
 * @author autogen
 *
 */
@Service
public class PeaFrequencyServiceImpl extends BaseServiceImpl<PeaFrequencyMapper, PeaFrequency,Integer> implements PeaFrequencyService {
    @Autowired
    private PeaFrequencyMapper cyMapper;
    @Autowired
    private SysClientEngMapper engMapper;

    @Override
    @Transactional(readOnly = true)
    public Map<String,Object> frequency(DateParameterBo da) {
        Map<String, Object> map = new LinkedHashMap<>();
        //所有用户均价值与均价值频数
//        da.setTable("avgValue");
        SysClientEng sysClientEng = engMapper.get(da.getId());
        if (sysClientEng != null && sysClientEng.getClientid() != null) {
            da.setTypeName(sysClientEng.getIdstytype());
        }
        //求出最近时间
        DateParameterBo d = cyMapper.latelyGetDate();
        da.setYear(d.getYear());
        da.setMonth(d.getMonth());

        List<PeaFrequency> listAll = cyMapper.frequency(da);
        List<PeaFrequency> listavg = new ArrayList<>();
        List<PeaFrequency> listpwrt = new ArrayList<>();
        List<PeaFrequency> listfgc = new ArrayList<>();
        List<PeaFrequency> listfee = new ArrayList<>();
        List<PeaFrequency> listtL = new ArrayList<>();
        PeaFrequency tmp;
        Map<String,String> tmap = new HashMap<>();
        if (!listAll.isEmpty()) {
            for (int i = 0; i < listAll.size(); i++) {
                tmp = listAll.get(i);
                if (tmp.getAvgvalue() != null) {
                    //将各区间字段去重
                    if(tmap.get("1_"+tmp.getAvgvalue() )== null ) {
                        PeaFrequency pf = new PeaFrequency();
                        pf.setAvgvalue(tmp.getAvgvalue());
                        pf.setAvgfrquency(tmp.getAvgfrquency());
                        listavg.add(pf);
                        tmap.put("1_"+tmp.getAvgvalue(),"1");
                    }
                }
                if (tmp.getPwrtvalue() != null) {
                    if(tmap.get("2_"+tmp.getPwrtvalue() )== null ) {
                        PeaFrequency pf = new PeaFrequency();
                        pf.setPwrtvalue(tmp.getPwrtvalue());
                        pf.setPwrtfrquency(tmp.getPwrtfrquency());
                        listpwrt.add(pf);
                        tmap.put("2_"+tmp.getPwrtvalue(),"2");
                    }
                }
                if (tmp.getFgcvalue() != null) {
                    if(tmap.get("3_"+tmp.getFgcvalue() )== null) {
                    PeaFrequency pf = new PeaFrequency();
                    pf.setFgcvalue(tmp.getFgcvalue());
                    pf.setFgcfrquency(tmp.getFgcfrquency());
                    listfgc.add(pf);
                    tmap.put("3_"+tmp.getFgcvalue(),"3");
                    }
                }
                if (tmp.getFeevalue() != null) {
                    if(tmap.get("4_"+tmp.getFeevalue() )== null ) {
                    PeaFrequency pf = new PeaFrequency();
                    pf.setFeevalue(tmp.getFeevalue());
                    pf.setFeefrquency(tmp.getFeefrquency());
                    listfee.add(pf);
                    tmap.put("4_"+tmp.getFeevalue(),"4");
                    }
                }
                if (tmp.gettLvalue() != null) {
                    if(tmap.get("5_"+tmp.gettLvalue() )== null) {
                    PeaFrequency pf = new PeaFrequency();
                    pf.settLvalue(tmp.gettLvalue());
                    pf.setTlfrquency(tmp.getTlfrquency());
                    listtL.add(pf);
                    tmap.put("5_"+tmp.gettLvalue(),"5");
                    }
                }
            }
        }
        map.put("listavg", listavg);
        map.put("listpwrt", listpwrt);
        map.put("listfgc", listfgc);
        map.put("listfee", listfee);
        map.put("listtL", listtL);
        return map;
    }


}
